{extend name="public/container"}
{block name="content"}
<div class="row">
    <div class="col-sm-12">
        <div class="ibox">
            <div class="ibox-title">
                <button type="button" class="btn btn-w-m btn-primary">自动代码生成器</button>
            </div>
            <div class="ibox-content">
                <div class="row">
                    <form class="layui-form" action="{:url('admin/system.curd/run')}" method="post">
                        <div class="layui-form-item">
                            <label class="layui-form-label" for="link_name">从数据表生成：</label>
                            <div class="layui-input-inline">
                                <select class="select db-table">
                                        <option value="">不从数据表生成</option>
                                        {foreach name='tables' item='table'}
                                        <option value="{$table.Name}" {$Request.param.table == $table.Name ? 'selected' : ''}>{$table.Name}</option>
                                        {/foreach}
                                </select> 
                            </div>
                            <button type="button" class="layui-btn  db-jump" title="点击此项选择从数据库生成字段">
                                    确认选择
                            </button>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label" for="link_name">生成文件：</label>
                        <div class="layui-input-inline">
                                <select name="file" class="select" required  lay-verify="required">
                                        <option value="all">默认生成文件（all）</option>
                                        <option value="controller">控制器（controller）</option>
                                        <option value="model">模型（model）</option>
                                        <option value="validate">验证器（validate）</option>
                                        {if condition='!$Request.param.table'}
                                            <option value="table">数据表（table）</option>
                                        {/if}
                                        <option value="edit">编辑添加页（edit.html）</option>
                                        <option value="index">列表页（index.html）</option>
                                        <option value="recycleBin">回收站（recyclebin.html）</option>
                                        <option value="form">搜索框（form.html）</option>
                                        <option value="th">表格表头（th.html）</option>
                                        <option value="td">表格表体（td.html）</option>
                                        <option value="config">配置文件（config.php）</option>
                                        <option value="dir">目录（dir）</option>
                                </select> 
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label" for="">模块：</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" required  lay-verify="required" placeholder="默认为当前模块" name="module" datatype="/^[a-z]+$/"  title="默认为当前模块">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label" for="">控制器：</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" required  lay-verify="required" placeholder="字母，驼峰式"  name="controller" datatype="/^[a-z]+$/" value="" title="默认为当前模块">
                        </div>
                    </div>

                    <h2 style="font-size: 16px">表单信息 ：</h2>
                    <hr>

                    <table class="layui-table">
                        <thead>
                            <tr class="text-c">
                                <th width="110" rowspan="2" title="删除后不可恢复，谨慎操作" style="text-align: center;">
                                    操作<br>
                                    <a href="javascript:;" class="layui-btn layui-btn-xs op-add" data-type="form" data-header="1" style="margin-top: 5px"> <span class="fa fa-plus"></span> 增加一栏</a>
                                </th>
                                <th width="660" colspan="5" title="字段配置信息">字段</th>
                                <th width="90" rowspan="2" title="勾选后自动给字段添加排序功能">筛选排序</th>
                                <th width="90" rowspan="2" title="勾选后字段可直接编辑修改数据库">是否可编辑</th>
                                <th width="220" colspan="2" title="自动生成搜索项">搜索</th>
                                <th width="630" colspan="4" title="如何使用请看相应文档"> 验证</th>
                            </tr>
                            <tr class="text-c">
                                <th width="120" title="中文描述，编辑页为对应label标签内容，首页对应表头内容"><span class="c-red">*</span> 标题</th>
                                <th width="120" title="一般为对应数据库字段的名称"><span class="c-red">*</span> 名称</th>
                                <th width="130" title="自动生成编辑页相应的表单控件"><span class="c-red">*</span> 类型</th>
                                <th width="170"
                                    title="只针对select,radio,checkbox控件,支持变量和配置值，例如 {foo}-对应conf.foo对应的配置项，生成foreach循环 | 1:值一#2:值二#3:值三 | 空值的默认标签名">
                                    选项值
                                </th>
                                <th width="120" title="字段编辑页默认值">默认值</th>
                                <th width="90" title="勾选后自动生成控制器筛选项和前端搜索框">表单搜索</th>
                                <th width="130" title="select的取值为字段中的选项值">搜索类型</th>
                          
                                <th width="180" title="layui的lay-verify配置项">是否必填</th>
                            </tr>
                            </thead>
                            <tbody id="tbody-form">
                                <tr>
                                    <td title="删除后不可恢复，谨慎操作">
                                        <a href="javascript:;" class="layui-btn layui-btn-xs  op-add" data-type="form">增加一栏</a>
                                        <a href="javascript:;" class="layui-btn  layui-btn-danger layui-btn-xs radius op-delete">删除</a>
                                    </td>
                                    <td title="中文描述，编辑页为对应label标签内容，首页对应表头内容">
                                        <input type="text" class="layui-input form-title" placeholder="中文描述" name="form[0][title]">
                                    </td>
                                    <td title="一般为对应数据库字段的名称">
                                        <input type="text" class="layui-input form-name" placeholder="字段，字母" name="form[0][name]">
                                    </td>
                                    <td title="自动生成编辑页相应的表单控件">
                                        <div class="select-box">
                                            <select class="select" name="form[0][type]">
                                                <option value="text">普通文本</option>
                                                <option value="hidden">隐藏</option>
                                                <option value="select">下拉选择</option>
                                                <option value="radio">单选</option>
                                                <option value="textarea">文本域</option>
                                                <option value="checkbox">多选</option>
                                                <option value="password">密码</option>
                                                <option value="number">数字</option>
                                                <option value="date">日期</option>
                                                <option value="dateRange">日期范围</option>
                                                <option value="dateTime">日期时间</option>
                                                <option value="dateTimeRange">日期时间范围</option>
                                                <option value="cityArea">地区三级联动</option>
                                                <option value="switches">开关</option>
                                                <option value="imageone">单图</option>
                                                <option value="images">多图</option>
                                            </select>
                                        </div>
                                    </td>
                                    <td title="只针对select,radio,checkbox控件,支持变量和配置值，例如 {foo}-对应conf.foo对应的配置项，生成foreach循环 | 1:值一#2:值二#3:值三 | 空值的默认标签名">
                                        <input type="text" class="layui-input" placeholder="变量或以#隔开" name="form[0][option]">
                                    </td>
                                    <td title="字段编辑页默认值">
                                        <input type="text" class="layui-input" placeholder="表单默认值" name="form[0][default]">
                                    </td>
                                    <td title="勾选后自动给字段添加排序功能">
    
                                            <input type="checkbox"  lay-skin="primary" name="form[0][sort]" value="1" title="排序">
                                        
                                    </td>
                                    <td title="勾选后字段可直接编辑">
    
                                        <input type="checkbox"  lay-skin="primary" name="form[0][edit]" value="1" title="可编辑">
                                    
                                    </td>
                                    <td class="text-c" title="勾选后自动生成控制器筛选项和前端搜索框">
                                        <input type="checkbox"  lay-skin="primary" name="form[0][search]" value="1" title="搜索">
                                    </td>
                                    <td title="select的取值为字段中的选项值">
                                        <div class="select-box">
                                            <select class="select" name="form[0][search_type]">
                                                <option value="text">text</option>
                                                <option value="select">select</option>
                                                <option value="date">date</option>
                                            </select>
                                        </div>
                                    </td>
                                    
                                    <td title="勾选后字段必填">
                                        <input type="checkbox"  lay-skin="primary" name="form[0][validate]" value="1" title="必填">
                                    </td>
                                </tr>
                                </tbody>
                       </table>

                       {if condition="!$Request.param.table"}
                       <h2 style="font-size: 16px">数据表信息 ：</h2>
                       <hr>
                       <div class="layui-form-item">
                           <label class="layui-form-label">数据表：</label>
                           <div class="layui-input-inline">
                               <input type="checkbox"  name="create_table" value="1"  title="创建数据表" lay-skin="primary" >
                               <input type="checkbox" name="create_table_force" value="1"  title="强制建表" lay-skin="primary" >
                           </div>
                           <button class="layui-btn op-sync"  title="将表单元素里的字段自动填充到表字段里，会清空原表字段的数据，谨慎操作" >同步字段</button>
                       </div>
   
                       <div class="layui-form-item">
                           <label class="layui-form-label">表引擎：</label>
                           <div class="layui-input-inline">
                               <select name="table_engine">
                                       <option value="InnoDB">InnoDB</option>
                                       <option value="MyISAM">MyISAM</option>
                                       <option value="MRG_MYISAM">MRG_MYISAM</option>
                                       <option value="MEMORY">MEMORY</option>
                                       <option value="ARCHIVE">ARCHIVE</option>
                               </select>
                           </div>
                           <div class="layui-input-inline">
                               <input type="text" required lay-verify="require" class="layui-input" name="table_name" placeholder="表名，不填则默认为控制器名.勿带表前缀">    
                           </div>
                       </div>
                       <table class="layui-table">
                               <thead>
                               <tr class="text-c">
                                   <th width="110" title="删除后不可恢复，谨慎操作" style="text-align: center">操作<br>
                                       <a href="javascript:;" style="margin-top: 5px" class="layui-btn layui-btn-xs  op-add" data-type="field" data-header="1"> <i class="fa fa-plus"></i> 增加一栏</a>
                                   </th>
                                   <th width="130" title="只能小写字符和下划线，例如 user_id"><span class="c-red">*</span> 名称</th>
                                   <th width="130" title="字段类型+大小，例如 varchar(255) , int(10) , text"><span class="c-red">*</span> 类型</th>
                                   <th width="130" title="为NULL表示不设默认值，不区分大小写">默认值</th>
                                   <th width="90" title="勾选后生成 NOT NULL">不是 null</th>
                                   <th width="90" title="勾选后生成索引">索引</th>
                                   <th width="130" title="设置字段备注">备注</th>
                                   <th width="130" title="扩展属性，例如 unsigned , auto_increment">扩展属性</th>
                               </tr>
                               </thead>
                               <tbody id="tbody-field">
                               <tr>
                                   <td title="删除后不可恢复，谨慎操作">
                                       <a href="javascript:;" class="layui-btn layui-btn-xs op-add" data-type="field">增加一栏</a>
                                       <a href="javascript:;" class="layui-btn layui-btn-xs layui-btn-danger op-delete">删除</a>
                                   </td>
                                   <td title="只能小写字符和下划线，例如 user_id">
                                       <input type="text" class="layui-input field-name" placeholder="字段名称" name="field[0][name]">
                                   </td>
                                   <td title="字段类型+大小，例如 varchar(255) , int(10) , text">
                                       <input type="text" class="layui-input" placeholder="例如varchar(255)" value="varchar(255)" name="field[0][type]">
                                   </td>
                                   <td title="为NULL表示不设默认值，不区分大小写">
                                       <input type="text" class="layui-input" placeholder="为NULL表示不设默认值" name="field[0][default]"
                                              value="NULL">
                                   </td>
                                   <td title="勾选后生成 NOT NULL">
                                       <input type="checkbox"  lay-skin="primary"  name="field[0][not_null]" value="1" title="NOT NULL">
                                   </td>
                                   <td title="勾选后生成索引">
                                           <input type="checkbox"  lay-skin="primary" name="field[0][key]" value="1" title="生成索引">
                                   </td>
                                   <td title="设置字段备注">
                                       <input type="text" class="layui-input field-comment" placeholder="备注" name="field[0][comment]">
                                   </td>
                                   <td title="扩展属性，例如 unsigned , auto_increment">
                                       <input type="text" class="layui-input" placeholder="例如unsigned" name="field[0][extra]">
                                   </td>
                               </tr>
                               </tbody>
                       </table>
                       {/if}
                       <!-- <h2 style="font-size: 16px">其他选项 ：</h2>
                       <hr>
                       <div class="layui-form-item">
                           <label class="layui-form-label">创建模型：</label>
                           <div class="layui-input-inline">
                                   <input type="checkbox" name="model" value="1"  title="创建模型" lay-skin="primary">
                                   <input type="checkbox" name="auto_timestamp" value="1"  title="自动时间戳" lay-skin="primary"  title="会自动创建相应的模型，并且自动添加字段create_time，update_time，并且开启时间戳记录">
                           </div>
                       </div>
                       <div class="layui-form-item">
                               <label class="layui-form-label">验证器：</label>
                               <div class="layui-input-inline">
                                       <input type="checkbox" name="validate" value="1"  title="创建验证器" lay-skin="primary">
                               </div>
                       </div> -->
   
                       <div class="layui-form-item">
                               <div class="layui-input-block">
                               <button class="layui-btn" lay-submit lay-filter="ajax_form_submit">立即提交</button>
                               </div>
                       </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    var form = layui.form;
      // 获取模板
      var template = {}, index = {};
        template['form'] = $("#tbody-form").html();
        template['field'] = $("#tbody-field").html();
        index['form'] = 0;
        index['field'] = 0;

    $(document).on("click", ".db-jump", function () {
        location.href = "/admin/system.curd/index/table/" + $('.db-table').val();
        }).on("click",".op-add",function(){
            var type = $(this).attr("data-type");
            var html = template[type].replace(/(\[\d+\])/g, '[' + (++index[type]) + ']');
            // 表头菜单，追加到第一个
            if ($(this)[0].hasAttribute('data-header')) {
                $("#tbody-" + type).prepend(html);
            } else {
                $(this).closest('tr').after(html);
            }
            form.render()
        }).on("click",".op-sync",function(){
  
            var objField = $("#tbody-field");
            objField.find('tr').remove();
            $("#tbody-form").find('tr').each(function () {
                objField.append(template['field'].replace(/(\[\d+\])/g, '[' + (++index['field']) + ']'));
                var objCurrent = objField.find('tr:last');
                objCurrent.find('.field-comment').val($(this).find('.form-title').val());
                objCurrent.find('.field-name').val($(this).find('.form-name').val());
            });
            form.render();
            return false;
        }).on("click", ".op-delete", function () {
            // 删除一栏
            $(this).closest("tr").fadeOut(undefined, undefined, function () {
                // 使用回调函数，强行移除该DOM
                $(this).remove();
            });
            form.render();
        })
        {present name="table_info"}
            var tableInfo = {$table_info|raw};
            var objForm = $("#tbody-form");
            objForm.find('tr').remove();
            for (var i = 0; i < tableInfo.length; i++) {
                objForm.append(template['form'].replace(/(\[\d+\])/g, '[' + (++index['form']) + ']'));
                var objCurrent = objForm.find('tr:last');
                objCurrent.find('.form-name').val(tableInfo[i]);
            }
        {/present}
</script>
{/block}